<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<HTML>
<HEAD>
	<TITLE> ZTREE DEMO - beforeEditName / beforeRemove / onRemove / beforeRename / onRename</TITLE>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../zTree/css/demo.css" type="text/css">
<link rel="stylesheet" href="../../zTree/css/metroStyle/metroStyle.css" type="text/css">
<script type="text/javascript" src="../../zTree/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="../../zTree/js/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="../../zTree/js/jquery.ztree.excheck-3.5.js"></script>
<script type="text/javascript" src="../../zTree/js/jquery.ztree.exedit-3.5.js"></script>
	<SCRIPT type="text/javascript">
		var setting = {
			view: {
				addHoverDom: addHoverDom,
				removeHoverDom: removeHoverDom,
				showLine: false,
				showIcon: false,
				checkable : true
			},
			edit: {
				enable: true,
				editNameSelectAll: true,
				showRemoveBtn: showRemoveBtn,
				showRenameBtn: showRenameBtn
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				beforeDrag: beforeDrag,
				beforeEditName: beforeEditName,
				beforeRemove: beforeRemove,
				beforeRename: beforeRename,
				onRemove: onRemove,
				onRename: onRename
			}
		};

		$(document).ready(function() {
			var treeNode;
			$.ajax({
				async : false,
				cache : false,
				type : 'POST',
				dataType : "json",
				url : "getReczsk",
				error : function() {
					alert('请求失败');
				},
				success : function(data) {
					treeNode = data;
				}
			});
			$.fn.zTree.init($("#treeDemo"), setting, treeNode);
		});
		
		function beforeDrag(treeId, treeNodes) {
			return false;
		}
		
		function beforeEditName(treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.selectNode(treeNode);
			setTimeout(function() {
				if (confirm("您确定修改 -- " + treeNode.name + " 节点吗")) {
					setTimeout(function() {
						zTree.editName(treeNode);
					}, 0);
				}
			}, 0);
			return false;
		}
		function beforeRemove(treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			zTree.selectNode(treeNode);
			var info= confirm("您确定删除" + treeNode.name + "节点吗？");
			if(info==true)
				$.ajax({
					async : false,
					cache : false,
					type : 'POST',
					data : {"id":treeNode.id,"pId":treeNode.id,"name":treeNode.name,"level":treeNode.level+1},
					dataType : "json",
					url : "deleteReczsk",
					error : function() {
						alert('请求失败');
					},
					success : function(data) {  
						if(data==0){
							alert("删除失败");
							return false;
						}
						if(data==1)
							return true;
					}
				});	
		}
		//改过了
		function onRemove(e, treeId, treeNode) {
			
		}
		//改过了
		function beforeRename(treeId, treeNode, newName, isCancel) {
			if (newName.length == 0) {
				setTimeout(function() {
					var zTree = $.fn.zTree.getZTreeObj("treeDemo");				
					zTree.cancelEditName();
					alert("名字不能为空");
				}, 0);
				return false;
			}
			else{
				$.ajax({
					async : false,
					cache : false,
					type : 'POST',
					data : {"id":treeNode.id,"pId":treeNode.id,"name":newName,"level":treeNode.level+1},
					dataType : "json",
					url : "updateReczsk",
					error : function() {
						alert('请求失败');
					},
					success : function(data) {   
						if(data==0){
							alert("修改失败");
							return false;
						}
						if(data==1)
							return true;
					}
				});	
			}		
		}
		//改过了
		function onRename(e, treeId, treeNode, isCancel) {
		}
		//改过了
		function showRemoveBtn(treeId, treeNode) {
			return !treeNode.isParent;
		}
		//改过了
		function showRenameBtn(treeId, treeNode) {
			return true;
		}
		
		function addHoverDom(treeId, treeNode) {
			var sObj = $("#" + treeNode.tId + "_span");
			if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0)
				return;
			var addStr = "<span class='button add' id='addBtn_" + treeNode.tId+ "' title='add node' onfocus='this.blur();'></span>";
			sObj.after(addStr);
			var btn = $("#addBtn_" + treeNode.tId);
			btn.bind("click", function() {		 
				var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				$.ajax({
					async : false,
					cache : false,
					type : 'POST',
					data : {"id":"","pId":treeNode.id,"name":"edit node","level":treeNode.level+2},
					dataType : "json",
					url : "addReczsk",
					error : function() {
						alert('请求失败');
					},
					success : function(data) {   
						if(data==null)
							alert("修改失败");						
						if(data!=null)
							zTree.addNodes(treeNode, {id : data.id,pId : data.pId,name : data.name,level : data.level});							
					}
				});					
				return false;
			});
		};
		//改过了
		function removeHoverDom(treeId, treeNode) {
			$("#addBtn_"+treeNode.tId).unbind().remove();
		};
	</SCRIPT>
</HEAD>

<BODY>
<div class="content_wrap">
	<div>
		<ul id="treeDemo" class="ztree" style="background-color:#FFFFFF;border: 0px;overflow-y:auto;overflow-x:auto;"></ul>
	</div>
</div>
</BODY>
</HTML>